package com.watson.onebox.algorithm.descarte;

import org.springframework.cglib.beans.BeanMap;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

public class ClassUtil {
    /**
     * @param object    旧的对象带值
     * @param addMap    动态需要添加的属性和属性类型
     * @param addValMap 动态需要添加的属性和属性值
     * @return 新的对象
     * @throws Exception
     */
    public Object dynamicClass(Object object, HashMap addMap, HashMap addValMap) throws Exception {
        HashMap returnMap = new HashMap();
        HashMap typeMap = new HashMap();


        Class<?> type = object.getClass();
        BeanInfo beanInfo = Introspector.getBeanInfo(type);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < propertyDescriptors.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();
            if (!propertyName.equals("class")) {
                Method readMethod = descriptor.getReadMethod();
                Object result = readMethod.invoke(object);
                //可以判断为 NULL不赋值
                returnMap.put(propertyName, result);
                typeMap.put(propertyName, descriptor.getPropertyType());
            }
        }

        returnMap.putAll(addValMap);
        typeMap.putAll(addMap);
        //map转换成实体对象
        DynamicBean bean = new DynamicBean(typeMap);
        //赋值
        Set keys = typeMap.keySet();
        for (Iterator it = keys.iterator(); it.hasNext(); ) {
            String key = (String) it.next();
            bean.setValue(key, returnMap.get(key));
        }
        Object obj = bean.getObject();
        return obj;
    }

    public Object getObjectValue(Class claz, Map<String, List<Object>> params) {
        Field[] declaredFields = claz.getDeclaredFields();
        HashMap typeMap = new HashMap();

        for (int i = 0; i < declaredFields.length; i++) {
            Field declaredField = declaredFields[i];
            typeMap.put(declaredField.getName(), declaredField.getType());
        }
        try {
            Class<?> tableVo = Class.forName(claz.getName());
            Object o = tableVo.newInstance();

            DynamicBean bean = new DynamicBean(o);
            Iterator<Map.Entry<String, List<Object>>> iterator = params.entrySet().iterator();
            Map<String, Integer> posMap = new HashMap<>();
            params.keySet().forEach(k -> {
                posMap.put(k, 0);
            });
            while (iterator.hasNext()) {
                Map.Entry<String, List<Object>> next = iterator.next();
                bean.setValue(next.getKey(), next.getValue().get(posMap.get(next.getKey())));
            }

            return bean.getObject();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }
}
